<script setup>
import {ref} from "vue";
import {useRouter} from "vue-router"
import request  from '@/api/request'
let router = useRouter();

let emits=defineEmits(["ref-table"])
let purchaseReturns=ref({})
let warehouse=ref([])
let purchaseOrder=ref([])
let returnDetails=ref([])
let User=ref([])



let onSubmit=()=>{
    purchaseReturns.value.returnDetailsList=returnDetails.value
    purchaseReturns.value.purchaseReturnsId="PR-"+new Date().getTime()
        request({
            url:"/purchasseReturns",
            method:"post",
            data:purchaseReturns.value
        }).then(res=>{
            emits("ref-table")
        })
}

let location=()=>{
    request({
        //查询员工
            url:"/system/user/list",
            method:"post",
            data:{
                current:1,
                size:1000
            }
        }).then(res=>{
            User.value=res.data.records
        })
    request({
        //查询仓库
        url:"/warehouse",
        method:"get",
        params:{
            current:1,
            size:1000
    }
    }).then(res=>{
        warehouse.value=res.data.date
    })

    request({
        //查询采购订单
        url:"/purchaseOrder",
        method:"get",
        params:{
            purchaseOrderState:'采购完成'}
        
    }).then(res=>{
        purchaseOrder.value=res.data.records
    })
}
location();

let a=(row)=>{
    request({
        //查询采购订单
        url:"/acceptanceOnArrival/selectT",
        method:"get",
        params:{
            acceptanceOnArrivalPurchaseOrderId:row}
        
    }).then(res=>{
        returnDetails.value=res.data
        purchaseReturns.value.purchaseReturnsNumberOfReturns=0
        purchaseReturns.value.purchaseReturnsReturnPrice=0
        for(let i = 0;i<returnDetails.value.length;i++){
            returnDetails.value[i].returnDetailsSupplierPricesId=returnDetails.value[i].supplierPrice.supplierPricesid;
            returnDetails.value[i].returnDetailsInboundDetailsId=returnDetails.value[i].inboundDetailsPurchaseDetailsId;
            returnDetails.value[i].returnDetailsNumberOfReturns=returnDetails.value[i].zong;
            returnDetails.value[i].returnDetailsNoNumberOfReturns=0;
            purchaseReturns.value.purchaseReturnsNumberOfReturns+=returnDetails.value[i].returnDetailsNumberOfReturns;
             purchaseReturns.value.purchaseReturnsReturnPrice+=returnDetails.value[i].returnDetailsNumberOfReturns*returnDetails.value[i].supplierPrice.supplierpricesStandardunitprice;
        }
    })
}

</script>
<template>
    <div>
        <el-card>
            <template #header>
                <el-form :model="purchaseReturns" ref="form" :inline="false" class="card_wrap">
                    <el-form-item label="采购单" prop="purchaseReturnsPurchaseOrderId" class="c_item">
                        <el-select class="sousuo"v-model="purchaseReturns.purchaseReturnsPurchaseOrderId" style="width: 150px;" @change="a">
                            <el-option v-for="item in purchaseOrder" :label="item.purchaseOrderName" :value="item.purchaseOrderId">
                                </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="退货仓库" prop="purchaseReturnsReturnsWarehouse" class="c_item">
                        <el-select class="sousuo"v-model="purchaseReturns.purchaseReturnsReturnsWarehouse" style="width: 150px;">
                            <el-option v-for="item in warehouse" :label="item.warehouseName" :value="item.warehouseId">
                                </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="退货数量" prop="purchaseReturnsNumberOfReturns" class="c_item">
                        <el-input v-model="purchaseReturns.purchaseReturnsNumberOfReturns" style="width:150px" disabled class="sousuo"></el-input>
                    </el-form-item>
                    <el-form-item label="退货金额" prop="purchaseReturnsReturnPrice" class="c_item">
                        <el-input v-model="purchaseReturns.purchaseReturnsReturnPrice" style="width:150px" disabled class="sousuo"></el-input>
                    </el-form-item>
                    <el-form-item label="确认退货" prop="purchaseReturnsConfirm" class="c_item">
                        <el-select class="sousuo"v-model="purchaseReturns.purchaseReturnsConfirm" style="width: 150px;">
                            <el-option label="已确认" value="已确认">
                            </el-option>
                            <el-option label="无需退货" value="无需退货">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="退货时间" prop="purchaseReturnsReturnTime" class="c_item">
                        <el-date-picker v-model="purchaseReturns.purchaseReturnsReturnTime" value-format="YYYY-MM-DD" type="date" placeholder="选择日期时间">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="经办人" prop="purchaseReturnsAttn" class="c_item">
                        <el-select class="sousuo"v-model="purchaseReturns.purchaseReturnsAttn" style="width: 150px;">
                            <el-option v-for="item in User" :label="item.nickname" :value="item.id">
                                </el-option>
                        </el-select>
                    </el-form-item>

                </el-form>

                <el-table :data="returnDetails" border stripe>
                    <el-table-column prop="returnDetailsInboundDetailsId" label="采购详情编码"/>
                    <el-table-column prop="returnDetailsNumberOfReturns" label="退货数量"/>
                    <el-table-column prop="returnDetailsNoNumberOfReturns" label="退货数量（未入库）"/>
                    <el-table-column prop="returnDetailsSupplierPricesId" label="供应商价格编码"/>
                </el-table>
                
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">立即添加</el-button>
                </el-form-item>
            </template>
        </el-card>
    </div>
</template>
<style scoped>

.card_wrap {
    width: 98%;
    display: flex;
    flex-wrap: wrap;
    justify-content:flex-start;
}
.c_item{
    flex:1;
    margin:0px 5px 15px 0;
    width: calc((100%-0px)/2); 
    min-width:calc((100% - 10px)/2);
    max-width:calc((100% - 10px)/2);
}
</style>